package com.yxhl.zoume.core.busticket.presenter;

import com.yxhl.protobuf.EndStationEntry;
import com.yxhl.protobuf.PubEndStationRequest;
import com.yxhl.protobuf.PubEndStationResponse;
import com.yxhl.protobuf.ScheduleType;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.busticket.model.ZMSelectStartCityEntity;
import com.yxhl.zoume.core.busticket.ui.info.ZMScheduleType;
import com.yxhl.zoume.core.busticket.view.BusArrivalStationView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.busticket.GetArrivalCitiesUseCase;

import java.util.List;

import javax.inject.Inject;

/**
 * 获取车站列表的presenter
 * Created by vhawk on 16/3/20.
 */
public class ArrivalCitiesPresenter implements IPresenter {

    private final GetArrivalCitiesUseCase useCase;
    private BusArrivalStationView busArrivalStationView;

    @Inject
    public ArrivalCitiesPresenter(GetArrivalCitiesUseCase useCase) {
        this.useCase = useCase;
    }

    @Override
    public void attachView(BaseView view) {
        busArrivalStationView = (BusArrivalStationView) view;
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        useCase.unsubscribe();
    }

    /**
     * 加载巴士列表
     *
     * @param selectStartCityEntity
     */
    public void fetchBusStations(ZMSelectStartCityEntity selectStartCityEntity) {
        busArrivalStationView.waitingForQueryCities(true);

        String startCityName = selectStartCityEntity.getStartCityName();
        String startCityCode = selectStartCityEntity.getStartCityCode();
        ScheduleType scheduleType = getScheduleType(selectStartCityEntity.getZmScheduleTypeList());

        PubEndStationRequest pubEndStationRequest = PubEndStationRequest
                .newBuilder()
                .setCityName(startCityName)
                .setCityCode(startCityCode)
                .setType(scheduleType)
                .build();

        useCase.execute(pubEndStationRequest, new ZMSubscriber<PubEndStationResponse>() {
            @Override
            public void onNext(PubEndStationResponse pubEndStationResponse) {
                super.onNext(pubEndStationResponse);
                List<EndStationEntry> endStationList = pubEndStationResponse.getEndStationEntryList();
                if (endStationList != null && endStationList.size() > 0) {
                    busArrivalStationView.renderBusStationListResult(endStationList);
                } else {
                    busArrivalStationView.waitingForQueryCities(false);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                busArrivalStationView.waitingForQueryCities(false);
            }
        });
    }

    private ScheduleType getScheduleType(List<ZMScheduleType> zmScheduleTypeList) {
        if (zmScheduleTypeList != null && zmScheduleTypeList.size() > 0) {
            ScheduleType scheduleType;

            ZMScheduleType zmScheduleType = zmScheduleTypeList.get(0);
            switch (zmScheduleType) {
                case V_DABA_BUS:
                    scheduleType = ScheduleType.V_DABA_BUS;
                    break;
                case V_DIY_BUS:
                    scheduleType = ScheduleType.V_DIY_BUS;
                    break;
                case V_XD_BUS:
                    scheduleType = ScheduleType.V_XD_BUS;
                    break;
                case V_FT_BUS:
                    scheduleType = ScheduleType.V_FT_BUS;
                    break;
                default:
                    scheduleType = ScheduleType.SCHEDULETYPE_DEFAULT;
            }

            return scheduleType;
        }
        return ScheduleType.V_XD_BUS;
    }
}
